﻿addLoadFuc(function() {
    if ($id("ipt_user_sel"))
        EventUtil.addHandler($id("ipt_user_sel"), "click", function() { initUlList(_per, ""); });
    if ($id("ipt_role_sel"))
        EventUtil.addHandler($id("ipt_role_sel"), "click", function() { initUlList(_role, ""); });
    if ($id("ipt_dep_sel"))
        EventUtil.addHandler($id("ipt_dep_sel"), "click", function () { initUlList(_org, ""); });
    initUlList(_org, "");
    if (!addExistedUsersToRightUl()) window.close();
});

var _org = "org";
var _role = "role";
var _per = "per";
var _rightUl = $id("id_ul_right");
//根据无序列表的名

function initUlList(ulType, name) {
    var dt;
    if (ulType == _org ) {
        dt = ns.GetOrgs(name).value;
        constructList(_org + "_list",dt,_org);
    } else if (ulType == _role ) {
        dt = ns.GetRoles(name).value;
        constructList(_role + "_list", dt, "rol");//prifex长度统一为3,方便去除
    } else if (ulType == _per ) {
        dt = ns.GetUsers(name).value;
        constructList(_per + "_list",dt,_per);
    } else {
        return;
    }
}

//构造无序列表
function constructList(ulId,dt,prifex) {
    if (dt == null || dt.Rows.length < 1) return;
    var parenDiv = $id("id_div_left");
    
    //如果已经构造了就不用再向数据库中查询了
    if ($id(ulId)) {
        if (parenDiv.children.length > 0) {
            for (var i = 0; i < parenDiv.children.length; i++) {
                if (parenDiv.children[i].id == ulId) {
                    parenDiv.children[i].style.display = "block";
                    //显示该列表的时候,由于先前可能选择过该元素,并且该元素的背景颜色被标示了,所以,此时应该去除该元素的背景,以免产生误导
                    var lis = $id(ulId).children;
                    if (lis && lis.length > 0) {
                        for (var index = 0; index < lis.length; index++) {
                            lis[index].style.backgroundColor = "#ffffff";
                        }
                    }
                } else {
                    parenDiv.children[i].style.display = "none";
                }
            }
        }
        return;
    }

    //隐藏其它元素(ul)
    if (parenDiv.children.length > 0) {
        for (var i = 0; i < parenDiv.children.length; i++) {
            parenDiv.children[i].style.display = "none";
        }
    }
    //如果没有该元素 则构造该元素
    var ul = document.createElement("ul");
    ul.id = ulId;
    parenDiv.appendChild(ul);
    for (var i = 0; i < dt.Rows.length; i++) {
        var li = document.createElement("li");
        li.innerHTML = "<a href='#' onclick=\"clickThis(this)\" >" + dt.Rows[i].TNAME + "</a>";
        li.id = prifex + dt.Rows[i].TID;//li的id带上它父级的前缀,使id唯一
        ul.appendChild(li);
    }
}

//-----记录最近一次左边点击的选项
var curLeftLi;
//-----记录最近一次右侧点击的选项
var curRightLi;
//--------将选项从左侧移动到右侧,移动后,将不会删除左侧信息.
function clickThis(a) {
    if (!a) return;
    var li = a.parentNode;
    
    if (li.parentNode.id == "id_ul_right") {
        curRightLi = li;
        var ulr = $id("id_ul_right");
        for (var i = 0; i < ulr.children.length;i++) {
            if (ulr.children[i].id == li.id) {
                ulr.children[i].style.backgroundColor = "#daa520";
            } else {
                ulr.children[i].style.backgroundColor = "#ffffff";
            }
        }
    } else {
        //标出选项颜色
        var ulr = a.parentNode.parentNode;
        for (var i = 0; i < ulr.children.length; i++) {
            if (ulr.children[i].id == li.id) {
                ulr.children[i].style.backgroundColor = "#9ed7ed";
            } else {
                ulr.children[i].style.backgroundColor = "#ffffff";
            }
        }

        curLeftLi = li;
    }
}
//----------------移动li项目-----------------------------------
//var comTypes = "comtype";
var comRelation = "relation";
var rightLiPrefix = "ru1";//右侧li的id前缀名
var rightLiCurMaxIdNum = 0;//右侧li里的数字后缀值
/*
右侧li的格式:
  <li comtype="org role per" id="_orgID_roleID_perID" relation='+++' ></li>
  comTypes为组合类型:如果只为组织类型,那 comtype="org"
  如果为:组织 角色 用户:那就为comtype="org role per" 
  中间 空格 分隔
  id的格式为 _组织id_角色id_用户id
  角色,组织和用户间之间的关系见 ../Engine/RemarkSection/权限备注.txt 文本的第一条.
  relation表示 "组织 角色 用户" 之间的关系. 一般情况下,对用同一个项关系为与的关系,当当relation
  中某个值的符号为减号 - ,说明是"除"次之外的关系,例如 relation='-++' 说明排除"组织"
  如果relation='++-' 说明排除具体的某人;
*/
function addToRight() {
    if (!curLeftLi) return;
    
    var curli = curLeftLi.id;
    var type = curli.substr(0, 3);// org rol or per?
    var id = curli.substr(3, curli.length - 3);
    var li = document.createElement("li");
    //设置id和comTypes
    if (type == _org) {
        li.id = "_" + id + "__";
       // li.setAttribute(comTypes, _org);
        li.setAttribute(comRelation, "+++");
    } else if(type=="rol") {
        li.id = "__" + id + "_";
       // li.setAttribute(comTypes, _role);
        li.setAttribute(comRelation, "+++");
    } else if(type==_per) {
        li.id = "___" + id;
      //  li.setAttribute(comTypes, _per);
        li.setAttribute(comRelation, "+++");
    } else {
        return;
    }
    //设置事件和展示数据
   // EventUtil.addHandler(li, "click", function() { clickThis.call(li, li); });
    li.innerHTML = curLeftLi.innerHTML;
    _rightUl.appendChild(li);
    //当有元素被直接移动到右侧时,设置该元素的背景,同时消除原先的元素背景
    curRightLi = li;
    for (var i = 0; i < _rightUl.children.length; i++) {
        if (_rightUl.children[i].id == li.id) {
            _rightUl.children[i].style.backgroundColor = "#daa520";
        } else {
            _rightUl.children[i].style.backgroundColor = "#ffffff";
        }
    }
    
}

//将数据从右侧移除
function addToLeft() {
    if (!curRightLi) return;
    curRightLi.parentNode.removeChild(curRightLi);
}


//右移并最佳该项(与) 不允许和人相与
function addToRightAnd() {
    if (!curLeftLi || !curRightLi) return;
    
    var li = curLeftLi;
    var type = li.id.substr(0, 3);// org rol or per?
    var id = li.id.substr(3, li.id.length - 3);
    if (type == _per) {
        alert("不能同时又 属于 某人!如果需要给某人赋予权限,请直接将其移入右侧即可!");
        return;
    }

    /*
     rlis   0   1    2     3 
     value     org  role  per
    */
    var rlis = curRightLi.id.split('_');
    
    if (rlis[1] != "" && rlis[2] != "") {
        alert("该选项已经包含了 组织和角色 ,若要添加新项请选择添加!");
        return;
    }
    
    /*
    <li comtype="org role per" id="_orgID_roleID_perID" ></li>
    */
   var rli = curRightLi;
   if (type == _org) {
       if (rlis[2] != "") {
           //rli.setAttribute(comTypes, "org role");
           rli.id = "_" + id + "_" + rlis[2] + "_";
           rli.setAttribute(comRelation, replaceCharAt(rli.getAttribute(comRelation), '+', 0));//将组织的关系位置替换为 加号 '+'
           rli.children[0].innerHTML += "[且]" + li.children[0].innerHTML;
           curLeftLi = undefined;
       }
   } else if (type == "rol") {
       if (rlis[1] != "") {
          // rli.setAttribute(comTypes, "org role");
           rli.id = "_" + rlis[1] + "_" + id + "_";
           rli.setAttribute(comRelation, replaceCharAt(rli.getAttribute(comRelation), '+', 1));
           rli.children[0].innerHTML += "[且]" + li.children[0].innerHTML;
           curLeftLi = undefined;
       }
       
   } 
}

//右移并"排除"该项
function addToRightNot() {
    if (!curLeftLi || !curRightLi) return;

    var rlis = curRightLi.id.split('_');// null org role per
    if (rlis[1] != "" && rlis[2] != "" && rlis[3] != "") return;

    var li = curLeftLi;
    var leftType = li.id.substr(0, 3);// org rol or per?
    var id = li.id.substr(3, li.id.length - 3);
    
    if (leftType == _org) {
        if (rlis[1] != "") {
            alert("不能对同一条记录重复添加组织项.");
            return;
        }
       // curRightLi.setAttribute(comTypes, "org " + curRightLi.getAttribute(comTypes));
        curRightLi.id = "_" + id + "_" + rlis[2] + "_"+rlis[3];
        curRightLi.setAttribute(comRelation, replaceCharAt(curRightLi.getAttribute(comRelation), '-', 0));
        curRightLi.children[0].innerHTML += "[排除]" + li.children[0].innerHTML;
        curLeftLi = undefined;
    }else if (leftType == "rol") {
        if (rlis[2] != "") {
            alert("不能对同一条记录重复添加角色项.");
            return;
        }
        //var ct = curRightLi.getAttribute(comTypes);
        //if (ct.indexOf(_org) > -1) ct = insertStringAfterPos(ct, 3, " rol");
        //else {
        //    ct = "rol " + ct;
        //}
      //  curRightLi.setAttribute(comTypes,ct);
        curRightLi.id = "_" + rlis[1]+ "_" + id + "_" + rlis[3];
        curRightLi.setAttribute(comRelation, replaceCharAt(curRightLi.getAttribute(comRelation), '-', 1));
        curRightLi.children[0].innerHTML += "[排除]" + li.children[0].innerHTML;
        curLeftLi = undefined;
    } else if(leftType==_per){
        if (rlis[3] != "") {
            alert("不能对同一条记录重复添加用户项.");
            return;
        }
      //  curRightLi.setAttribute(comTypes, curRightLi.getAttribute(comTypes) + " per");
        curRightLi.id = "_" + rlis[1] + "_" + rlis[2] + "_" + id;
        curRightLi.setAttribute(comRelation, replaceCharAt(curRightLi.getAttribute(comRelation), '-', 2));
        curRightLi.children[0].innerHTML += "[排除]" + li.children[0].innerHTML;
        curLeftLi = undefined;
    }
    
}

//保存数据
function btnSaveData() {
    var p = window.opener;
    if (!p) return;
    var actId = getParamValue("curid");
    if (!actId) return;
    //----------start to set user data to xmlarea : wfuser - wfexpress - wfdesc 
    var lis = _rightUl.children;
    if (!lis) return;
    var count = lis.length;
    var innerData = "";
    var innerShowData = "";
    for (var i = 0; i < count; i++) {
        innerData += "<wfuser id='" + actId + "user" + i + "'><wfexpress>" + lis[i].id + lis[i].getAttribute(comRelation) + "</wfexpress><wfdesc>" + lis[i].firstChild.innerHTML + "</wfdesc></wfuser>";
        innerShowData += lis[i].firstChild.innerHTML + "<br />";
    }
    p.document.getElementById(actId).querySelector(tagwfusers).innerHTML = innerData;
    p.document.getElementById("lb_user_names").innerHTML = innerShowData;
    window.close();
}

//退出该界面
function btnCancel() {
    this.close();
}

//当用户从流程设计器中打开此界面时 应该将原先已经存在的用户放到右侧的 ul中

function addExistedUsersToRightUl() {
    var p = window.opener;
    if (!p) return  false;
    var actId = getParamValue("curid");
    if (!actId) return false;
    var users = p.document.getElementById(actId).querySelector(tagwfusers);
    if (users.children.length < 1) return true;
    var lis = users.children;
    var count = lis.length;
    if (!_rightUl) _rightUl = $id("id_ul_right");
    //右侧li格式: <li id="_1_4_4"  relation=""><a onclick="clickThis(this)" href="#"></a></li>
    for (var i = 0; i < count; i++) {
        var li = document.createElement("li");
        var exp = lis[i].querySelector(tagwfexpress).innerHTML;
        li.id = exp.substr(0, exp.length - 3);
        li.setAttribute(comRelation, exp.substr(exp.length - 3, 3));
        li.innerHTML = "<a onclick=\"clickThis(this)\" href='#'>" + lis[i].querySelector(tagwfdesc).innerHTML + "</a>";
        _rightUl.appendChild(li);
    }
    return true;
}